#include <iostream>
#include <iterator>
#include <algorithm>
#include <list>
#include <fstream>

int main()
{
  std::list<int> data;

  std::ifstream ifile("bit_seq.txt");

  std::copy(std::istream_iterator<int> (ifile),
	    std::istream_iterator<int> (),
	    std::back_inserter(data) );
  ifile.close();
  
  int count = 0;
  std::list<int>::iterator it = data.begin();

  while (it != data.end())
    {
      if (*it == 0)
	{
	  count = 0;
	}
      else 
	{
	  count++;
	  if (count == 5)
	    {
	      ++it;
	      data.insert(it, 0);
	      count = 0;
	    }
	}
      ++it;
    }

  std::ofstream ofile("bit_stff.txt");
  std::copy(data.begin(), data.end(), std::ostream_iterator<int>(ofile, " "));
  ofile.close();

  std::copy(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
}
